package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by Rays on 2019/11/23.
 */
public class CustomView extends View {

    private Paint paint = new Paint();
    private Paint paint2;

    Path path = new Path();

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        paint2 = new Paint(paint);
        paint2.setStrokeWidth(5);
        paint2.setColor(Color.BLACK);
        paint2.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        paint2.setStyle(Paint.Style.STROKE);
        paint2.setColor(Color.BLACK);
        int startX = 100;
        int startY = height >> 1;
        canvas.drawLine(50, startY, width - 100, startY, paint2);
        canvas.drawLine(startX, startY + 50, startX, 100, paint2);

        paint2.setStyle(Paint.Style.FILL);
        int arrOffset = 20;
        path.moveTo(startX - arrOffset, 100);
        path.lineTo(startX + arrOffset, 100);
        path.lineTo(startX, 100 - arrOffset);
        path.close();
        canvas.drawPath(path, paint2);

        path.moveTo(width - 100, startY - arrOffset);
        path.lineTo(width - 100, startY + arrOffset);
        path.lineTo(width - 100 + arrOffset, startY);
        path.close();
        canvas.drawPath(path, paint2);
        path.reset();

        paint2.setColor(Color.RED);
        paint2.setStyle(Paint.Style.STROKE);
        int radioX = 200;
        int radioY = radioX << 1;
        path.moveTo(startX, startY);
        path.quadTo(startX + radioX, startY - radioY, startX + (radioX << 1), startY);
        canvas.drawPath(path, paint2);
        canvas.save();

        canvas.translate(radioX << 1, 0);
        path.reset();
        path.moveTo(startX, startY);
        path.quadTo(startX + radioX, startY + radioY, startX + (radioX << 1), startY);
        canvas.drawPath(path, paint2);

//        canvas.translate(radioX << 1, 0);
        canvas.restore();
        path.reset();
        path.moveTo(startX, startY);
        path.quadTo(startX + radioX, startY + radioY, startX + (radioX << 1), startY);
        canvas.drawPath(path, paint2);

//        paint2.setColor(Color.GREEN);
//        canvas.drawCircle(width >> 1, height >> 1, 200, paint2);
//        canvas.drawCircle(width >> 1, height >> 1, 150, paint);
//
//        int startY = (height >> 1) - 300;
//        paint2.setColor(Color.BLUE);
//        canvas.drawLine(0, startY, width, startY, paint2);

//        startY += 100;
//
//        if (startY > height) {
//            startY = 50;
//        }
//
//        postInvalidateDelayed(1000);
    }
}
